<template>
  <!-- 容器 -->
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/shit.jpg" alt class="home-logo" />
        <span>后台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 网页主体内容 -->
    <el-container>
      <!-- 左侧 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 显示和隐藏菜单栏的按钮 -->
          <div class="toggle-button" @click="toggleCollpapse">
            |||
          </div>
        <!-- 
            菜单 unique-opened:只打开一个菜单, collapse：是否水平折叠收起菜单
            router: 启动路由，当点击对应的menu-item时将跳转到index值的路由
            default-active="index的值" 高亮值为index的项
        -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isCollapse"
         :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 每一个v-for都尽量提供一个唯一的key属性 动态数据绑定-->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" 
            :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  //渲染页面 将接口返回的数据挂载到组件的私有数据data中
  data() {
    return {
      //左侧菜单栏数据 返回的数据使用两个for循环将数据渲染出菜单栏
      menulist: [],
      //自定义菜单图标
      iconObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      //是否折叠菜单栏
      isCollapse: false,
      //激活链接的地址
      activePath: '',

    };
  },
  //created：生命周期函数 当本组件一创建时执行
  created() {
    //获取菜单栏信息
    this.getMenuList();
    //从sessionStorage获取激活地址
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    //退出登录
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有的菜单栏数据
    async getMenuList() {
      //get()函数返回的时一个promise,为了使其简化操作 需要加await/async
      const { data: res } = await this.$http.get("menus");
      //获取不成功
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      //成功
      this.menulist = res.data;
    },
    //显示和隐藏菜单栏
    toggleCollpapse(){
        //取反
        this.isCollapse =! this.isCollapse;
    },
    //保存链接状态到sessionStorage
    saveNavState(activePath){
        window.sessionStorage.setItem('activePath', activePath);
        this.activePath = activePath;
    },
  }
};
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex; /*弹性盒子*/
  justify-content: space-between; /*左右贴边*/
  padding-left: 0;
  align-items: center; /*上下居中 纵向居中*/
  color: #fff; /*设置文本颜色*/
  /*嵌套写法*/
  > div {
    display: flex;
    align-items: center;
    img {
      height: 50px;
      border-radius: 50%;
    }
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
      border-right: none; /*取消不对其的框框*/
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
    margin-right: 5px;
}
.toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>